package com.gitee.l0km.codegen.decorator;

import java.util.List;
import java.util.Map;

import org.apache.commons.cli.Options;

import com.gitee.l0km.codegen.base.AbstractSchema;
import com.gitee.l0km.codegen.base.generator.Generator;
import com.gitee.l0km.codegen.base.generator.GeneratorConfiguration;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

public class DecoratorGenerator extends Generator {
	private final DecoratorOptions options = DecoratorOptions.getInstance();
	private final DecoratorConfiguration config = new DecoratorConfiguration();
	public DecoratorGenerator() {
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	protected Map<String, List<? extends AbstractSchema>> generateTask() {
		ImmutableList.Builder<Decorator<?>> builder = ImmutableList.builder();
		List<Class<?>> interfaceClasses = config.getInterfaceClasses();
		List<Class<?>> refClasses = config.getRefClasses();
		for(int i = 0; i<interfaceClasses.size(); ++i){
			Decorator newSourceInfo = new Decorator(interfaceClasses.get(i), i < refClasses.size() ? refClasses.get(i) : null)
					.setExcludeAnnotations(config.getExcludeAnnotations())
					.setPortPrefix(config.getPortPrefix());
			if (!newSourceInfo.compile()){
				System.exit(1);
			}
			builder.add(newSourceInfo);
		}
		return ImmutableMap.<String, List<? extends AbstractSchema>>of(".",builder.build());
	}

	@Override
	protected GeneratorConfiguration getGeneratorConfiguration() {
		return config;
	}
	@Override
	protected Options getOptions() {
		return options.getOptions();
	}
	public static void main(String[] args) {
		new DecoratorGenerator().parseCommandLine(args).initEngine().multiGenerate();
	}
}
